package xyz.wadewhy.basicstest.easyExcel.color

import com.alibaba.excel.write.style.row.AbstractRowHeightStyleStrategy
import org.apache.poi.ss.usermodel.CellType
import org.apache.poi.ss.usermodel.Row
import kotlin.math.ceil


/**
 *@ClassName CellRowHeightStyleStrategy
 *@Description 业务用途
 *@Author zhongzihao
 *@Date 2022/3/18 3:06 PM
 *@Version 1.0
 **/
class CellRowHeightStyleStrategyx : AbstractRowHeightStyleStrategy() {
    private val DEFAULT_HEIGHT = 400

    /**
     * cell 中每行最多 100 个字
     */
    private val Cell_ONE_LINE_MAX_CHAR = 100

    override fun setHeadColumnHeight(row: Row, relativeRowIndex: Int) {}

    override fun setContentColumnHeight(row: Row, relativeRowIndex: Int) {
        val cellIterator = row.cellIterator()
        if (!cellIterator.hasNext()) {
            return
        }
        // 默认为 1行高度
        var maxHeight = 5
        while (cellIterator.hasNext()) {
            val cell = cellIterator.next()
            if (cell.cellType == CellType.STRING) {
                val length = ceil(cell.stringCellValue.toByteArray().size * 1.0 / Cell_ONE_LINE_MAX_CHAR)
                if (length > 1) {
                    maxHeight = maxHeight.coerceAtLeast(length.toInt())
                    row.height = (maxHeight * DEFAULT_HEIGHT).toShort()
                }
            }
        }
    }
}
